<template>
  <view class="container">
    <view class="header">
      <text class="title">编辑学生</text>
    </view>

    <view class="form">
      <view class="form-item">
        <text class="label">姓名</text>
        <input class="input" v-model="form.name" placeholder="请输入姓名" />
      </view>
      
      <view class="form-item">
        <text class="label">年龄</text>
        <input class="input" type="number" v-model="form.age" placeholder="请输入年龄" />
      </view>
      
      <view class="form-item">
        <text class="label">性别</text>
        <radio-group @change="handleGenderChange">
          <label class="radio-label">
            <radio value="男" :checked="form.gender === '男'" />
            <text>男</text>
          </label>
          <label class="radio-label">
            <radio value="女" :checked="form.gender === '女'" />
            <text>女</text>
          </label>
        </radio-group>
      </view>
      
      <view class="form-item">
        <text class="label">专业</text>
        <input class="input" v-model="form.major" placeholder="请输入专业" />
      </view>
    </view>

    <button class="delete-btn" @click="deleteStudent">删除学生</button>
    <button class="update-btn" @click="updateStudent">更新信息</button>
  </view>
</template>

<script>
import mockData from '@/static/mock-data.js';

export default {
  data() {
    return {
      studentId: null,
      form: {
        id: null,
        name: '',
        age: '',
        gender: '男',
        major: ''
      }
    };
  },
  onLoad(options) {
    this.studentId = parseInt(options.id);
    this.loadStudent();
  },
  methods: {
    loadStudent() {
      const student = mockData.getStudents().find(s => s.id === this.studentId);
      if (student) {
        this.form = {...student};
      }
    },
    handleGenderChange(e) {
      this.form.gender = e.detail.value;
    },
    updateStudent() {
      // 简单验证
      if (!this.form.name) {
        uni.showToast({ title: '请输入姓名', icon: 'none' });
        return;
      }
      
      if (!this.form.age) {
        uni.showToast({ title: '请输入年龄', icon: 'none' });
        return;
      }
      
      if (!this.form.major) {
        uni.showToast({ title: '请输入专业', icon: 'none' });
        return;
      }
      
      // 更新学生
      if (mockData.updateStudent({
        ...this.form,
        age: parseInt(this.form.age)
      })) {
        uni.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 1500,
          success: () => {
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          }
        });
      }
    },
    deleteStudent() {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除该学生吗？此操作不可恢复',
        success: (res) => {
          if (res.confirm) {
            if (mockData.deleteStudent(this.studentId)) {
              uni.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1500,
                success: () => {
                  setTimeout(() => {
                    uni.navigateBack();
                  }, 1500);
                }
              });
            }
          }
        }
      });
    }
  }
};
</script>

<style>
.container {
  padding: 30rpx;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.header {
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
  margin-bottom: 40rpx;
}

.title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
}

.form {
  background-color: white;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  margin-bottom: 30rpx;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
}

.label {
  width: 120rpx;
  font-size: 32rpx;
  color: #333;
}

.input {
  flex: 1;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 32rpx;
}

.radio-group {
  display: flex;
}

.radio-label {
  display: flex;
  align-items: center;
  margin-right: 50rpx;
}

.delete-btn {
  height: 90rpx;
  line-height: 90rpx;
  background-color: #ff4d4f;
  color: white;
  font-size: 36rpx;
  border-radius: 45rpx;
  margin-bottom: 30rpx;
}

.update-btn {
  height: 90rpx;
  line-height: 90rpx;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  font-size: 36rpx;
  border-radius: 45rpx;
}
</style>